package cn.appsys.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.pojo.DevUser;
import cn.appsys.service.devuser.DevUserService;
import cn.appsys.tools.Constants;


@Controller
@RequestMapping(value="/dev")
public class DevUserController {
	private Logger logger = Logger.getLogger(DevUserController.class);
	@Resource
	private DevUserService devUserService;
	
	@RequestMapping(value="/login")
	public String devlogin(){
		return "devlogin";
	}
	
	@RequestMapping(value="/dologin")
	public Object dologin(@RequestParam String devCode,@RequestParam String devPassword,
			            HttpServletRequest request,HttpSession session){
		logger.debug("aaaaaaaaaaaaaaaaaaaaaaaaaa");
		DevUser user = devUserService.login(devCode, devPassword);
		if (user != null) {
			session.setAttribute(Constants.DEV_USER_SESSION, user);
			return "redirect:/dev/main";
		}else {
			request.setAttribute("error", "用户名或密码错误");
			return "devlogin";
		}
		
	}
	
	@RequestMapping(value="/main")
	public Object main(HttpSession session){
		if (session.getAttribute(Constants.DEV_USER_SESSION) == null) {
			return "redirect:/dev/login";
		}
		return "dev/main";
	}
	
	@RequestMapping(value="/logout")
	public Object logout(HttpSession session){
		session.removeAttribute(Constants.DEV_USER_SESSION);
		return "devlogin";
		
	}
	
	
	
	
	
	
	
	
	
	
	
}
